Skip to main content

MainBridge

MainBridge class allows you to export objects to renderer.

danger

There should be only one MainBridge class instance in whole application!

MainBridge.constructor()

Creates MainBridge instance.

MainBridge.Export(name, object)

Exports specified object to renderer.

Arguments

NameTypeDescription
namestringName of the exported object
objectSee Supported TypesObject to export

Example

const { MainBridge } = require("electronbb");let mainBridge = new MainBridge();mainBridge.Export("testObject", {    exportedNumber: 1234,    exportedString: "abcd",    exportedFunction: () => {        console.log("Omg I can export functions!");    },});

You can access the object later from renderer

const { RendererBridge } = require("electronbb");let rendererBridge = new RendererBridge();const object = rendererBridge.GetSync("testObject");console.log(object.exportedString); // abcd

MainBridge.Delete(name)

Deletes speficied exported object only from main process.

Arguments

NameTypeDescription
nameString-

Example

In main process:

const { MainBridge } = require("electronbb");let mainBridge = new MainBridge();// let's export an objectmainBridge.Export("truth", {    rustSuperiorToCpp: true,});// oh, wait actually nomainBridge.Delete("truth");//there you gomainBridge.Export("truth", {    rustSuperiorToCpp: false, // I still probably will have to learn it, like I tried learning Rust like 3 fcking times and it's just so hard, someone plz help me});

In renderer process:

const { RendererBridge } = require("electronbb");let rendererBridge = new RendererBridge();const truth = rendererBridge.GetSync("truth"); // name same as in exportconsole.log(truth.rustSuperiorToCpp); // false

async MainBridge.Get(window, name)

Gets object exported from renderer.

Arguments

NameTypeDescription
windowBrowserWindowWindow to get object from
namestringName of the object to get

Returns

Your object.

Remarks

caution

Functions imported from renderer are converted to async functions due to Electron's limitations.

Example

See Intro#importing-an-object-from-renderer-in-main

static MainBridge.GetInstance()

Returns

Instance of MainBridge.
If none initialized, returns null;

Remarks

This will only return the latest initialized instance of MainBridge.

Example

const { MainBridge } = require("electronbb");
let mainBridge = new MainBridge();

console.log(mainBridge === MainBridge.GetInstance()); // true